package net.cn_su.sharpeyes.activity;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import net.cn_su.sharpeyes.R;
import net.cn_su.sharpeyes.application.SuApplication;
import net.cn_su.sharpeyes.beans.Organization;
import net.cn_su.sharpeyes.pref.ServerInfo_;
import net.cn_su.sharpeyes.utils.http.HttpClient;
import net.cn_su.sharpeyes.utils.http.HttpUrl;
import net.cn_su.sharpeyes.utils.http.JacksonMapper;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Fullscreen;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.codehaus.jackson.type.TypeReference;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * Created by yzy on 2016/10/20.
 */
@EActivity(R.layout.activity_first)
@Fullscreen
public class FirstActivity extends AppCompatActivity {
    @Pref
    protected ServerInfo_ serverInfo;

    @App
    protected SuApplication suApplication;

    @AfterViews
    protected void initData() {

        HttpUrl.setROOT(serverInfo.serverUrl().get()+"/api/");

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!serverInfo.isLogin().get()){
                    LoginActivity_.intent(FirstActivity.this).start();
                }else {
                    //判断是否有申请通过的组织
                    getApplyList();
                }
                finish();
            }
        }, 2000)
        ;
    }


    private void getApplyList() {

        RequestParams params = new RequestParams();
        params.add("user_id", serverInfo.userId().get().toString());

        HttpClient.get(HttpUrl.GET_ORG_APPLY, params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                OrgChooseActivity_.intent(FirstActivity.this).extra("flag", true).start();
                finish();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {

           //     Logger.v("result==" + responseString);
                List<Organization> organizationList = JacksonMapper.parseToList(responseString, new TypeReference<List<Organization>>() {
                });


                //已申请通过的列表
                List<Organization> mApplyList = getApplyOrganizations(organizationList);

                if (mApplyList.size() == 0) {
                    OrgChooseActivity_.intent(FirstActivity.this).extra("flag", true).start();
                    finish();
                }else {
                    Organization org=mApplyList.get(0);
                    LiveActivity_.intent(FirstActivity.this).extra("orgId", org != null ? org.getId() : -1).start();
                    finish();
                }


            }
        });


    }




    /**
     * 得到申请通过的列表
     *
     * @param orgs
     * @return
     */
    public List<Organization> getApplyOrganizations(List<Organization> orgs) {
        List<Organization> organizations = new ArrayList<>();
        // 将镇进行一次处理, 作为村的点来处理
        List<Organization> children = null;
        for (Organization org : orgs) {
            if (org.getChildren() != null && org.getChildren().size() > 0) {
                children = new ArrayList<>();
                for (Organization val : org.getChildren()) {
                    if (val.getStatus() == 1) {
                        children.add(val);
                    }
                }
                organizations.addAll(children);
            }
        }
        suApplication.setOrganizations(organizations);
        return organizations;
    }
}
